import json
import re
import time

import pymysql
import requests
from selenium import webdriver

def geturl(str):
    url="https://search.douban.com/movie/subject_search?search_text=" + str
    driver = webdriver.Edge()
    driver.get(url)
    req=driver.page_source
    obj = re.compile('<div class="title"><a href="(?P<url>.*?)" data-moreurl="onclick=&quot;', re.S)
    ret = obj.finditer(req)
    index=1
    for it in ret:
        if index==1:
            return it.group("url")
def databox():
    with open('trend.json', 'r', encoding='utf-8') as fp:
         # 输出结果是 <class '_io.TextIOWrapper'> 一个文件类对象

        # load()函数将fp(一个支持.read()的文件类对象，包含一个JSON文档)反序列化为一个Python对象
        data = json.load(fp)
        result=[]
        temptuple=(data.get("data")[0].get('totalBoxoffice'),data.get("data")[1].get('totalBoxoffice'),data.get("data")[2].get('totalBoxoffice'),data.get("data")[3].get('totalBoxoffice'),data.get("data")[4].get('totalBoxoffice'),data.get("data")[5].get('totalBoxoffice'),data.get("data")[5].get('businessDay'))
        result.append(temptuple)
        # print(data.get('data')[-1].get("businessDay"))
        # print(data.get('data')[-1].get('totalBoxoffice'))
    return result

def top10():
    with open('top10.json', 'r', encoding='utf-8') as fp:
        # 输出结果是 <class '_io.TextIOWrapper'> 一个文件类对象

        # load()函数将fp(一个支持.read()的文件类对象，包含一个JSON文档)反序列化为一个Python对象
        data = json.load(fp)
        result = []
        for index in data.get('data').get('top10Films'):
            temptuple = (index.get("filmName"), index.get('daySales'))
            result.append(temptuple)
            # print(data.get('data')[-1].get("businessDay"))
            # print(data.get('data')[-1].get('totalBoxoffice'))
            temptuple = ()
    return result
def downloadtop10():
    t = str(int(time.time()))
    url = "https://www.zgdypw.cn/data/searchDayBoxOffice.json?timestamp=" + t
    req = requests.get(url)
    #filename = url.split('/')[-3]+'.json'
    filename="top10.json"
    if req.status_code != 200:
        print('下载异常')
        return
    try:
        with open(filename, 'wb') as f:
            # req.content为获取html的内容
            f.write(req.content)
            print('下载成功')
    except Exception as e:
        print(e)

def downloadtrend():
    t = str(int(time.time()))
    url = 'https://www.zgdypw.cn/data/searchSevenDaysBoxOffice.json?timestamp='+t
    req = requests.get(url)
    filename="trend.json"
    if req.status_code != 200:
        print('下载异常')
        return
    try:
        with open(filename, 'wb') as f:
            # req.content为获取html的内容
            f.write(req.content)
            print('下载成功')
    except Exception as e:
        print(e)


def gettop10url():
    result=[]
    for movie in top10():
        result.append(geturl(movie[0]))
    return result

def trend_sql():
    conn = pymysql.connect(host="139.9.219.187",port=3306,user="lh",passwd="159753",db="movie_big_data",charset='utf8')
    # 使用cursor()方法获取操作游标
    cursor = conn.cursor()
    try:
        sql="update echarts_line_trend_box_office set six_days_ago = '%s', five_days_ago = '%s',four_days_ago='%s',three_days_ago='%s',two_days_ago='%s',one_days_ago='%s',last_day='%s'"% databox()[0]
        cursor.execute(sql)
        conn.commit()
    except Exception as e:
        print(e)
        print("录入失败")
        conn.rollback()
    finally:
        conn.close()
        cursor.close()

def top10_sql():
    conn = pymysql.connect(host="139.9.219.187", port=3306, user="lh", passwd="159753", db="movie_big_data",charset='utf8')
    # 使用cursor()方法获取操作游标
    cursor = conn.cursor()
    try:
        sql_delete="delete from movie_rank_box_office where day_box_office is not null"
        sql_insert="insert into movie_rank_box_office ( name ,day_box_office) values (%s,%s)"
        #sql = "update echarts_line_trend_box_office set six_days_ago = '%s', five_days_ago = '%s',four_days_ago='%s',three_days_ago='%s',two_days_ago='%s',one_days_ago='%s',last_day='%s'" % ("1","1","1","1","1","1","2022-01-03")
        cursor.execute(sql_delete)
        #cursor.execute(sql,databox()[0])

        cursor.executemany(sql_insert,top10())
        conn.commit()
    except Exception as e:
        print(e)
        print("录入失败")
        conn.rollback()
    finally:
        conn.close()
        cursor.close()
